package cn.zhc.com.http.download;

import android.content.Context;
import android.support.annotation.NonNull;

import com.liulishuo.filedownloader.BaseDownloadTask;
import com.liulishuo.filedownloader.FileDownloadListener;
import com.liulishuo.filedownloader.FileDownloader;

import java.io.File;

import cn.zhc.com.http.listener.OnHttpDownLoadListener;
import cn.zhc.com.utils.UtilsFile;
import cn.zhc.com.utils.UtilsLog;
import cn.zhc.com.utils.UtilsOther;

/**
 * Created by zhouhongchun on 2017/8/9.
 * 下载文件专用
 */

public class HttpDownload {

    /**
     * 获取下载文件的存放目录
     */
    public static String getDownloadDirectory(Context context){
        return UtilsFile.getFileByDirectoryName(context, UtilsOther.getAPPName(context)+File.separator).getAbsolutePath();
    }

    /**
     *
     * @param downloadUrl 下载链接建议提前 URLEncoder.encode(urlParams,"UTF-8");
     * @param downloadDirectory
     * @param fileName 文件名字，不需要后缀文件格式
     * @param fileSuffix 后缀文件格式
     * @param mOnHttpDownLoadListener
     */
    public static void requestDownload(@NonNull String downloadUrl, @NonNull String downloadDirectory, @NonNull String fileName, @NonNull String fileSuffix, final OnHttpDownLoadListener mOnHttpDownLoadListener){
        String filePath = downloadDirectory + fileName + fileSuffix;
        BaseDownloadTask mBaseDownloadTask = FileDownloader.getImpl().create(downloadUrl)
                .setPath(filePath);
        mBaseDownloadTask.setListener(new FileDownloadListener() {
            @Override
            protected void pending(BaseDownloadTask task, int completedBytes, int totalBytes) {
                UtilsLog.d(UtilsLog.TAG, "下载任务的唯一id"+task.getId());
                mOnHttpDownLoadListener.downLoadPending(task,completedBytes,totalBytes);

            }

            @Override
            protected void connected(BaseDownloadTask task, String etag, boolean isContinue, int completedBytes, int totalBytes) {
                mOnHttpDownLoadListener.downloadConnected(task, etag,isContinue,completedBytes,totalBytes);
            }

            @Override
            protected void progress(BaseDownloadTask task, int completedBytes, int totalBytes) {
                mOnHttpDownLoadListener.downloadProgress(task,completedBytes,totalBytes);
            }

            @Override
            protected void blockComplete(BaseDownloadTask task) {
                mOnHttpDownLoadListener.downloadBlockComplete(task);
            }

            @Override
            protected void retry(final BaseDownloadTask task, final Throwable ex, final int retryingTimes, final int completedBytes) {
                mOnHttpDownLoadListener.downloadRetry(task,ex,retryingTimes,completedBytes);
            }

            @Override
            protected void completed(BaseDownloadTask task) {
                mOnHttpDownLoadListener.downloadCompleted(task);
            }

            @Override
            protected void paused(BaseDownloadTask task, int completedBytes, int totalBytes) {
                mOnHttpDownLoadListener.downLoadPaused(task,completedBytes,totalBytes);
            }

            @Override
            protected void error(BaseDownloadTask task, Throwable e) {
                mOnHttpDownLoadListener.downLoadError(task,e);
            }

            @Override
            protected void warn(BaseDownloadTask task) {
                mOnHttpDownLoadListener.downLoadWarn(task);
            }
        }).start();

    }

    public static void requestPauseDownload(int id){
        FileDownloader.getImpl().pause(id);
    }

    public static void requestClearDownload(int id,@NonNull String fileDirectory,@NonNull String fileName){
        FileDownloader.getImpl().clear(id,fileDirectory + fileName);
    }
}
